/* Microstepping demo This requires that microstep control pins be connected in addition to STEP,DIR Copyright (C)2015 Laurentiu Badea Modified By: Yasir Shafiullah This file may be redistributed under the terms of the MIT license. A copy of this license has been included with this distribution in the file LICENSE. */ #include // MOTOR 1 (X Axis) // Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step #define MOTOR_STEPS 200 #define MOTOR_STEPS_MOVE 20 #define MOTOR_STEPS_MOVE_STOP 8 #define RPM 30 #define DIR 3 #define STEP 4 #define SLEEP 2 // optional (just delete SLEEP from everywhere if not used) #include #define txPin 12 // PA0(MISO) transmit signal to the bridge #define rxPin 13 // PA1(SCK) recieves signal from bridge SoftwareSerial mySerial(rxPin, txPin); /* Choose one of the sections below that match your board */ #include "DRV8834.h" #define M0 7 #define M1 8 DRV8834 stepper(MOTOR_STEPS, DIR, STEP, SLEEP, M0, M1); DRV8834 steppermove(MOTOR_STEPS_MOVE, DIR, STEP, SLEEP, M0, M1); char Incoming_value = 0; //Variable for storing Incoming_value char Incoming_storage_move = 0; //Variable for storing Incoming_value const int buttonPinx = 15; int buttonStatex; // #include "A4988.h" // #define MS1 10 // #define MS2 11 // #define MS3 12 // A4988 stepper(MOTOR_STEPS, DIR, STEP, SLEEP, MS1, MS2, MS3); // #include "DRV8825.h" // #define MODE0 10 // #define MODE1 11 // #define MODE2 12 // DRV8825 stepper(MOTOR_STEPS, DIR, STEP, SLEEP, MODE0, MODE1, MODE2); // #include "DRV8880.h" // #define M0 10 // #define M1 11 // #define TRQ0 6 // #define TRQ1 7 // DRV8880 stepper(MOTOR_STEPS, DIR, STEP, SLEEP, M0, M1, TRQ0, TRQ1); // #include "BasicStepperDriver.h" // generic // BasicStepperDriver stepper(DIR, STEP); // MOTOR 2 (Y Axis) #include const int dirPin = 16; const int stePin = 17; const int sleePin = 18; const int buttonPiny = 14; int buttonStatey; void setup() { mySerial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission pinMode(txPin, INPUT); //default tx as input pinMode(5, OUTPUT); //Sets digital pin 8 as output pin /* Set target motor RPM. */ // if using enable/disable on ENABLE pin (active LOW) instead of SLEEP uncomment next line // stepper.setEnableActiveState(LOW); // set current level (for DRV8880 only). // Valid percent values are 25, 50, 75 or 100. // stepper.setCurrent(100); //Motor 1 (X Axis) // initialize the pushbutton pin as an input: pinMode(buttonPinx, INPUT); //Motor 2 (Y Axis) pinMode(stePin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(sleePin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPiny, INPUT); } void loop() { if (mySerial.available() > 0) { Incoming_value = mySerial.read(); //Read the incoming data and store it into variable Incoming_value mySerial.print(Incoming_value); //Print Value of Incoming_value in Serial monitor mySerial.print("\n"); //New line if (Incoming_value == 'A') //Checks whether value of Incoming_value is equal to A { stepper.begin(RPM); stepper.enable(); motor4(); } else if (Incoming_value == 'B') //Checks whether value of Incoming_value is equal to B { stepper.begin(RPM); stepper.enable(); motor8(); } else if (Incoming_value == 'C') //Checks whether value of Incoming_value is equal to C { stepper.begin(RPM); stepper.enable(); motor12(); } else if (Incoming_value == 'D') //Checks whether value of Incoming_value is equal to D { stepper.begin(RPM); stepper.enable(); motor16(); } else if (Incoming_value == 'E') //Checks whether value of Incoming_value is equal to E { stepper.begin(RPM); stepper.enable(); motor20(); } else if (Incoming_value == 'F') //Checks whether value of Incoming_value is equal to F { stepper.begin(RPM); stepper.enable(); motor24(); } else if (Incoming_value == 'G') //Checks whether value of Incoming_value is equal to G { stepper.begin(RPM); stepper.enable(); motor28(); } else if (Incoming_value == 'H') //Checks whether value of Incoming_value is equal to H { stepper.begin(RPM); stepper.enable(); motor32(); } else if (Incoming_value == 'I') //Checks whether value of Incoming_value is equal to I { stepper.begin(RPM); stepper.enable(); motor36(); } else if (Incoming_value == 'J') //Checks whether value of Incoming_value is equal to J { stepper.begin(RPM); stepper.enable(); motor40(); } else if (Incoming_value == 'K') //Checks whether value of Incoming_value is equal to K { stepper.begin(RPM); stepper.enable(); motor44(); } else if (Incoming_value == 'L') //Checks whether value of Incoming_value is equal to L { stepper.begin(RPM); stepper.enable(); motor48(); } else if (Incoming_value == 'M') //Checks whether value of Incoming_value is equal to M { stepper.begin(RPM); stepper.enable(); motor52(); } else if (Incoming_value == 'N') //Checks whether value of Incoming_value is equal to N { stepper.begin(RPM); stepper.enable(); motor56(); } else if (Incoming_value == 'O') //Checks whether value of Incoming_value is equal to O { stepper.begin(RPM); stepper.enable(); motor60(); } else if (Incoming_value == 'P') //Checks whether value of Incoming_value is equal to P { stepper.begin(RPM); stepper.enable(); motor64(); } else if (Incoming_value == 'Q') //Checks whether value of Incoming_value is equal to Q { stepper.begin(RPM); stepper.enable(); motor68(); } else if (Incoming_value == 'R') //Checks whether value of Incoming_value is equal to R { stepper.begin(RPM); stepper.enable(); motor72(); } else if (Incoming_value == 'S') //Checks whether value of Incoming_value is equal to S { stepper.begin(RPM); stepper.enable(); motor76(); } else if (Incoming_value == 'T') //Checks whether value of Incoming_value is equal to T { stepper.begin(RPM); stepper.enable(); motor80(); } else if (Incoming_value == 'U') //Checks whether value of Incoming_value is equal to U { stepper.begin(RPM); stepper.enable(); motor84(); } else if (Incoming_value == 'V') //Checks whether value of Incoming_value is equal to V { stepper.begin(RPM); stepper.enable(); motor88(); } else if (Incoming_value == 'W') //Checks whether value of Incoming_value is equal to W { steppermove.begin(170); steppermove.enable(); Incoming_storage_move=Incoming_value; motorforward(); } else if (Incoming_value == 'X') //Checks whether value of Incoming_value is equal to X { steppermove.begin(170); steppermove.enable(); Incoming_storage_move=Incoming_value; motorbackward(); } else if (Incoming_value == 'Y') //Checks whether value of Incoming_value is equal to Y { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, LOW); motorleft(); } else if (Incoming_value == 'Z') //Checks whether value of Incoming_value is equal to Z { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); motorright(); } else if (Incoming_value == '1') //Checks whether value of Incoming_value is equal to 1 { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); motor_b3cm(); } else if (Incoming_value == '2') //Checks whether value of Incoming_value is equal to 2 { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); motor_b6cm(); } else if (Incoming_value == '3') //Checks whether value of Incoming_value is equal to 3 { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); motor_b9cm(); } else if (Incoming_value == '4') //Checks whether value of Incoming_value is equal to 4 { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); motor_b12cm(); } else if (Incoming_value == '5') //Checks whether value of Incoming_value is equal to 5 { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); motor_b15cm(); } else if (Incoming_value == '6') //Checks whether value of Incoming_value is equal to 6 { Incoming_storage_move=Incoming_value; digitalWrite(sleePin, HIGH); digitalWrite(dirPin, LOW); motorleft(); steppermove.begin(170); steppermove.enable(); motororigin(); } } } void motor4() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(190.98); // forward revolution } void motor8() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(381.96); // forward revolution } void motor12() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(572.94); // forward revolution } void motor16() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(763.92); // forward revolution } void motor20() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(954.9); // forward revolution } void motor24() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(1145.88); // forward revolution } void motor28() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(1336.86); // forward revolution } void motor32() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(1527.84); // forward revolution } void motor36() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(1718.82); // forward revolution } void motor40() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(1909.8); // forward revolution } void motor44() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(2100.78); // forward revolution } void motor48() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(2291.76); // forward revolution } void motor52() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(2482.74); // forward revolution } void motor56() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(2673.72); // forward revolution } void motor60() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(2864.7); // forward revolution } void motor64() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(3055.68); // forward revolution } void motor68() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(3246.66); // forward revolution } void motor72() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(3437.64); // forward revolution } void motor76() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(3628.62); // forward revolution } void motor80() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(3819.6); // forward revolution } void motor84() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(4010.58); // forward revolution } void motor88() { digitalWrite(5, LOW); stepper.setMicrostep(1); // Set microstep mode to 1:1 stepper.rotate(4201.56); // forward revolution } void motorforward() { steppermove.setMicrostep(8); while (Incoming_storage_move == 'W') { steppermove.move(8*MOTOR_STEPS_MOVE); Incoming_value = mySerial.read(); if (Incoming_value == '0') { break; } else { steppermove.move(8*MOTOR_STEPS_MOVE); } } digitalWrite(5, HIGH); steppermove.disable(); } void motorbackward() { steppermove.setMicrostep(8); while (Incoming_storage_move == 'X') { steppermove.move(-8*MOTOR_STEPS_MOVE); Incoming_value = mySerial.read(); // read the state of the pushbutton value: buttonStatex = digitalRead(buttonPinx); if (Incoming_value == '0') { break; } else if (buttonStatex == HIGH) { break; } else { steppermove.move(-8*MOTOR_STEPS_MOVE); } } steppermove.disable(); steppermove.begin(170); steppermove.enable(); steppermove.setMicrostep(1); steppermove.move(MOTOR_STEPS_MOVE/3); digitalWrite(5, HIGH); } void motorleft() { // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); while (buttonStatey == LOW) { // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); Incoming_value = mySerial.read(); if (buttonStatey == HIGH) { break; } else if (Incoming_value == '0') { break; } } digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); for (int x = 0; x <200; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); } digitalWrite(sleePin, LOW); digitalWrite(5, HIGH); } void motorright() { // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); while (buttonStatey == LOW) { // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); Incoming_value = mySerial.read(); if (buttonStatey == HIGH) { break; } else if (Incoming_value == '0') { break; } } digitalWrite(sleePin, HIGH); digitalWrite(dirPin, LOW); for (int x = 0; x <200; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); } digitalWrite(sleePin, LOW); digitalWrite(5, HIGH); } void motor_b3cm() { for (int x = 0; x <3000; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); if (buttonStatey == HIGH) { break; } } digitalWrite(sleePin, LOW); digitalWrite(5, HIGH); } void motor_b6cm() { for (int x = 0; x <6000; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); if (buttonStatey == HIGH) { break; } } digitalWrite(sleePin, LOW); digitalWrite(5, HIGH); } void motor_b9cm() { for (int x = 0; x <9000; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); if (buttonStatey == HIGH) { break; } } digitalWrite(sleePin, LOW); digitalWrite(5, HIGH); } void motor_b12cm() { for (int x = 0; x <12000; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); if (buttonStatey == HIGH) { break; } } digitalWrite(sleePin, LOW); digitalWrite(5, HIGH); } void motor_b15cm() { for (int x = 0; x <15000; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); // read the state of the pushbutton value: buttonStatey = digitalRead(buttonPiny); if (buttonStatey == HIGH) { break; } } digitalWrite(sleePin, LOW); digitalWrite(5, HIGH); } void motororigin() { steppermove.setMicrostep(8); while (Incoming_storage_move == '6') { steppermove.move(-8*MOTOR_STEPS_MOVE); Incoming_value = mySerial.read(); // read the state of the pushbutton value: buttonStatex = digitalRead(buttonPinx); if (Incoming_value == '0') { break; } else if (buttonStatex == HIGH) { break; } else { steppermove.move(-8*MOTOR_STEPS_MOVE); } } steppermove.disable(); steppermove.begin(170); steppermove.enable(); steppermove.setMicrostep(1); steppermove.move(MOTOR_STEPS_MOVE/3); digitalWrite(5, HIGH); }